// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import router from './router'
import App from './App'
import iView from 'iview'
import echarts from 'echarts'
import 'iview/dist/styles/iview.css'
import '@/config/global'
import BaiduMap from 'vue-baidu-map'
import 'echarts-wordcloud'
import VueBus from 'vue-bus'


require('@/assets/css/common.css')
require('echarts-wordcloud');

Vue.config.productionTip = false
//添加原型方法
Vue.prototype.dateShift = function (offset) {
  let now = new Date()
  let newTime = new Date(now.getTime() + (offset * 24 * 60 * 60 * 1000))
  let obj = {
    year: newTime.getFullYear(),
    month: (newTime.getMonth() + 1 < 10) ? '0' + (newTime.getMonth() + 1) : newTime.getMonth() + 1,
    day: newTime.getDate() < 10 ? '0' + newTime.getDate() : newTime.getDate()
  }
  return `${obj.year}-${obj.month}-${obj.day}`
}
Vue.prototype.stringWrap = function (str, indexes) {
  var newstr = "";
  for (var i = 0; i < str.length; i += indexes) {
    var tmp = str.substring(i, i + indexes);
    newstr += tmp + '\n';
  }
  return newstr;
}

Vue.prototype.StringEllipse = function (str, length) {
  return str.substring(0, length) + '...';
}

Vue.prototype.findMaxValue = function (arr, keyOfValue) {
  let max = null;
  let matrix = [];
  if (arr instanceof Array) {
    matrix = arr.map((o) => {
      if (typeof o[keyOfValue] === 'number') {
        return o[keyOfValue];
      } else {
        throw o[keyOfValue] + ' is not a valid Number';
      }
    })
    matrix = matrix.sort((a, b) => {
      return a - b;
    });
    max = matrix[matrix.length - 1];
  } else {
    throw 'The arr params of function findMaxValue is not an Array';
  }
  return max;
}

Vue.prototype.findMinValue = function (arr, keyOfValue) {
  let min = null;
  let matrix = [];
  if (arr instanceof Array) {
    matrix = arr.map((o) => {
      if (typeof o[keyOfValue] === 'number') {
        return o[keyOfValue];
      } else {
        throw o[keyOfValue] + 'is not a valid Number';
      }
    })
    matrix = matrix.sort((a, b) => {
      return a - b;
    });
    min = matrix[0];
  } else {
    throw 'The arr params of function findMinValue is not an Array';
  }
  return min;
}

Vue.prototype.$Crender = function (id, option) {
  let wrapper = document.getElementById(id)
  if (wrapper) {
    let width = wrapper.clientWidth
    let height = wrapper.clientHeight
    wrapper.style.width = width + 'px'
    wrapper.style.height = height + 'px'
    let chart = echarts.init(wrapper)
    chart.setOption(option)
    chart.resize()
    window.resize = function () {
      chart.resize()
    }
    return chart
  }

}
//注册过滤器
Vue.filter('nullHandle', function (val) {
  val = val || '暂无信息';
  return val;
})
//注册使用插件
Vue.use(iView, {
  transfer: true,
  size: 'large',
  select: {
    arrow: 'md-arrow-dropdown',
    arrowSize: 20
  }
});
Vue.use(VueBus)
Vue.use(BaiduMap, {
  ak: '7I6eFbjHbRo9PTClcG7VDzamxC9ct4il'
})
//注册iview目前无法自动注册的全局组件
Vue.component('i-time-line-item', iView.TimelineItem)
Vue.component('i-ym-submenu', iView.Submenu)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  next();
});

router.afterEach(route => {
  iView.LoadingBar.finish();
});
